package com.kiln.juxingqs.ui

import android.content.Intent
import android.view.View
import androidx.core.view.isVisible
import androidx.recyclerview.widget.DividerItemDecoration
import client.global.commlib.toast.toast
import client.global.commlib.widget.EmptyLayout
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.ZrLots1Adapter
import com.kiln.juxingqs.api.bean.GoodsDetail
import com.kiln.juxingqs.api.bean.TradeGoodsDetail
import com.kiln.juxingqs.api.bean.Zcjl
import com.kiln.juxingqs.databinding.ActivityZrLots1Binding
import com.kiln.juxingqs.dialog.JyInputNumDialog
import com.kiln.juxingqs.dialog.MsgTitleDialog
import com.kiln.juxingqs.ui.vm.ZrLotsViewModel

/**
 * 转让详情-明细1(第一次资产)改 提货商品
 */
class ZrLotsActivity :
    BaseMiddleActivity<ZrLotsViewModel, ActivityZrLots1Binding>() {

    override fun getLayout(): Int {
        return R.layout.activity_zr_lots1
    }

    val adapter: ZrLots1Adapter by lazy {
        ZrLots1Adapter(this)
    }

    override fun initViewModel(): ZrLotsViewModel = createViewModel()

    var goods_id: String? = ""
    var goods_name: String? = ""
    var deal_num: String? = ""
    var r_borrow_num: String? = ""
    var pick_type: String? = ""
    var title_name: String? = ""
    var goods_price: String? = ""
    var price_pf: String? = ""
    var f_price: String? = ""
    var g_price: String? = ""
    var r_label: Int = 0
    var ddd_type: Int = 0
    var is_dj_show: Int = 0
    var user_dict: TradeGoodsDetail.UserDict? = null

    override fun initView() {
        binding?.apply {
            event = this@ZrLotsActivity
            vm = viewModel
            goods_id = intent.getStringExtra("goods_id")
            goods_name = intent.getStringExtra("goods_name")
            deal_num = intent.getStringExtra("deal_num")
            pick_type = intent.getStringExtra("pick_type")
            goods_price = intent.getStringExtra("goods_price")
            price_pf = intent.getStringExtra("price_pf")
            f_price = intent.getStringExtra("f_price")
            g_price = intent.getStringExtra("g_price")
            title_name = intent.getStringExtra("title_name")
            r_borrow_num = intent.getStringExtra("r_borrow_num")

            ddd_type = intent.getIntExtra("ddd_type", 0)
            r_label = intent.getIntExtra("r_label", 0)
            is_dj_show = intent.getIntExtra("is_dj_show", 0)

            user_dict = intent.getParcelableExtra("user_dict")

        }
        title_name?.let {
            binding.titleName = it
        }



        binding.apply {
            binding.kyzNum = ""
            when (pick_type) {
                "1" -> {
                    llAsset1.isVisible = true
                    r_borrow_num?.let {
                        binding.kyzNum = " (可预支数量:$it)"
                    }
                }

                "2" -> { //冻结点对点
                    llAsset2.isVisible = true
                    btnAsset2DjDdd.isVisible = is_dj_show == 1 //1显示 0隐藏

                }

                "3" -> llTihuo.isVisible = true
                "4" -> llKjyPaip.isVisible = true
                else -> {
                    llTotalMenu.isVisible = false
                }
            }
        }


        val decoration = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
        val dividerDrawable = resources.getDrawable(R.drawable.drawable_divider)
        decoration.setDrawable(dividerDrawable)
        binding.recyclerView.addItemDecoration(decoration)
        binding.recyclerView.adapter = adapter

        binding.refreshLayout.setOnRefreshListener {
            adapter.hasMoreState.value = true
            viewModel.pageOne.value = 0
            viewModel.jYzcjl(goods_id!!, r_label)
        }
        binding.recyclerView.onLoadMore = {
            if (adapter.hasMoreState.value!! && !viewModel.isOneLoading.value!!) {
                viewModel.jYzcjl(goods_id!!, r_label)
            }
        }
        binding.emptyLayout.setOnClickListener {
            viewModel.pageOne.value = 0
            viewModel.jYzcjl(goods_id!!, r_label)
        }
        viewModel.isOneLoading.observe(this, {
            if (!it) {
                binding.refreshLayout.isRefreshing = false
            }
        })

//        binding.incZrDetailTitle.apply {
//            tv_column3.text = "剩余"
//            tv_column4.visibility = View.GONE
//
//        }


    }

    override fun onResume() {
        super.onResume()
        viewModel.pageOne.value = 0
        viewModel.jYzcjl(goods_id!!, r_label)
    }

    override fun initData() {


//        adapter.hasMoreState.value = false
//        binding.emptyLayout.visibility = View.GONE
//        adapter.submitList(ArrayList<GetDddBatchRet.Batch>().apply {
//            add(
//                GetDddBatchRet.Batch(
//                    0, 0, 0, "", "", "",
//                    deal_num!!.toInt(), 0, 0, 0, 0, 0,
//                    "", "", "", "", ""
//                )
//            )
//        })
//        binding.refreshLayout.isRefreshing = false

    }

    override fun observerUI() {
        viewModel.retOne.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    val list =
                        it.data?.record_list as MutableList<Zcjl.Record?>
                    if (viewModel.pageOne.value == 1) {
                        adapter.submitList(list)
                    } else
                        adapter.appendList(list)
                    if (list != null && list.size > 0) {
                        if (list.size < 20) {
                            adapter.hasMoreState.value = false
                        }
                        binding.emptyLayout.visibility = View.GONE
                    } else {
                        if (adapter.itemCount == 0)
                            binding.emptyLayout.errorType = EmptyLayout.NODATA
                        adapter.hasMoreState.value = false
                    }
                } else {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })
        viewModel.queRenRet.observe(this, {
            it.let {
                if (it.isSuccess) {
                    toast(it.message)
                }
            }
        })
        //特价买
        viewModel.buyRet.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast(it.message)
                    initData()
                }
            }
        })
        viewModel.zzRet.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast("成功")
                    finish()
                } else toast(it.message)
            }
        })

    }

    //申请提货
    fun applyDelivery() {
        startActivity(Intent(this, ApplyDeliveryActivity::class.java).apply {
            putExtra("goods_id", goods_id)
            putExtra("goods_name", goods_name)
            putExtra("deal_num", deal_num)
            putExtra("pick_type", pick_type)
        })

    }

    //冻结点对点
    fun djDianDdian() {
        startActivity(Intent(this, ZrXyNewActivity::class.java).apply {
            putExtra("goods_id", goods_id)
            putExtra("goods_name", goods_name)
            putExtra("deal_num", deal_num)
            putExtra("goods_price", goods_price)
            putExtra("ddd_type", 2)
            putExtra("titleName", "冻结点对点")
            putExtra("user_dict", user_dict)
            putExtra("is_dj_show", is_dj_show)
        })

    }

    fun zz() {
        JyInputNumDialog(this).apply {
            ok = { num ->
                viewModel.holdZz(num, goods_id)
                dismiss()
            }
        }.show()
    }


    //点对点收购
    fun goPoint1PointBuy() {
        startActivity(Intent(this, XyzrNewActivity::class.java).apply {
            putExtra("index", 1)
            putExtra("titleName", "点对点收购")
            putExtra("goods_id", goods_id)//新goods_id

        })
    }

    //商品一:20 商品二:21 特价券:22 可交易商品:23
    fun detail() {
        startActivity(Intent(this, XyzrNewActivity::class.java).apply {
            putExtra("detail_type", "20")
            putExtra("goods_id", goods_id)//新goods_id
        })
    }


    //商品购买
    fun lotBuy() {
//        val bannerList = intent.getParcelableArrayListExtra<GoodsDetail.Banner>("banner_list")
//        val detailList = intent.getParcelableArrayListExtra<GoodsDetail.Detail>("detail_list")
        startActivity(Intent(this, LotDeal2Activity::class.java).apply {
            putExtra("goods_id", goods_id)
//            putExtra("banner_list", bannerList)
//            putExtra("detail_list", detailList)

        })
    }

    /**
     * 预支点对点
     */
    fun yzDdd() {
        startActivity(Intent(this, ZrXyNewActivity::class.java).apply {
            putExtra("goods_id", goods_id)
            putExtra("goods_name", goods_name)
            putExtra("deal_num", deal_num)
            putExtra("goods_price", goods_price)
            putExtra("titleName", "预支点对点")
            putExtra("ddd_type", 1)

        })
    }

    /**
     * 一键转存
     */
    fun oneClickTransfer() {
        MsgTitleDialog(this).apply {
            okTxt = "确认"
            cancleTxt = "取消"
            title = "是否一键转存？"
            ok = {
                viewModel.oneTouchRedeposit(goods_id!!)
            }
        }.show()

    }

    //当前价购买 / 特价购买
    fun goDifTypeBuy(type: Int) {
        when (type) {
            1 -> {//特价购
                JyInputNumDialog(this).apply {
                    ok = { num ->
                        viewModel.holdBuyTj(goods_id!!, price_pf!!, num)
                        dismiss()
                    }
                }.show()
            }
        }

        /* startActivity(Intent(this, XyzrNewActivity::class.java).apply {
             putExtra("index", 1)
             putExtra("titleName", "")

         })*/

    }


    //挂单服务中心（协议转让）
    fun goGdFwzx() {
        startActivity(
            Intent(this, ZrXyNewActivity::class.java).apply {
                putExtra("goods_id", goods_id)
                putExtra("goods_name", goods_name)
                putExtra("goods_price", goods_price)
                putExtra("f_price", f_price)
                putExtra("deal_num", deal_num)
                putExtra("ZrXyType", "server")
                putExtra("serverType", 1)
                putExtra("serverName", "服务中心")
                putExtra("titleName", "挂单服务中心")
            })

    }


    //挂单供应商
    fun goGdGys() {
        startActivity(
            Intent(this, ZrXyNewActivity::class.java).apply {
                putExtra("goods_id", goods_id)
                putExtra("goods_name", goods_name)
                putExtra("goods_price", goods_price)
                putExtra("g_price", g_price)
                putExtra("deal_num", deal_num)
                putExtra("ZrXyType", "server")
                putExtra("serverType", 2)
                putExtra("serverName", "供应商")
                putExtra("titleName", "挂牌供应商")
            })
    }

    //点对点交易
    fun goZrDddjy() {
        startActivity(Intent(this, ZrXyNewActivity::class.java).apply {
            putExtra("goods_id", goods_id)
            putExtra("goods_name", goods_name)
            putExtra("goods_price", goods_price)
            putExtra("deal_num", deal_num)
            putExtra("titleName", "点对点交易")
        })
    }


}